﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Demo.Site;
using Demo.Model;

namespace Demo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private SiteBase siteBase = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            dgvSearchResult.AutoGenerateColumns = false;
            cmbSiteType.DataSource = Enum.GetNames(typeof(SiteType));
            cmbSiteType.SelectedIndexChanged += new EventHandler(cmbSiteType_SelectedIndexChanged);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSiteType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSiteType.SelectedIndex != -1)
            {
                SiteType siteType = (SiteType)Enum.Parse(typeof(SiteType), cmbSiteType.Text);
                siteBase = SiteBase.CreateSite(siteType);
                this.dgvSearchResult.DataBindings.Add("DataSource", siteBase, "Companys");
                txtSearch.Focus();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (siteBase != null)
                {
                    siteBase.SearchCompany(txtSearch.Text, SearchCompanyCompleted);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSearchResult_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Company company = siteBase.Companys[e.RowIndex];
                if (company != null)
                    siteBase.ReadCompanyDetail(company, CompanyDetail.登记信息, ReadCompanyDetailCompleted);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="siteBase"></param>
        private void SearchCompanyCompleted(SiteBase siteBase)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="company"></param>
        private void ReadCompanyDetailCompleted(Company company)
        {

        }

        private void dgvSearchResult_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
